package com.major.crowd.controller;

import com.major.crowd.service.api.MemberService;
import com.major.crowd.constant.CrowdConstant;
import com.major.crowd.po.MemberPO;
import com.major.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberProviderController {

    @Autowired
    private MemberService memberService;

    /**
     * 增
     * @param memberPO
     * @return
     */
    @RequestMapping("save/member/remote")
    ResultEntity<String> saveMember(@RequestBody MemberPO memberPO){

        try {
            //成功
            memberService.saveMember(memberPO);
            return ResultEntity.successWithoutData();
        } catch (Exception e) {
            e.printStackTrace();

            //失败-账号重复
            if (e instanceof DuplicateKeyException){
                return ResultEntity.failed(CrowdConstant.MASSAGE_LOGIN_ACCT_ALREADY_IN_USED);
            }
            //失败
            return ResultEntity.failed(e.getMessage());
        }
    }

    /**
     * 查
     * @param loginacct
     * @return
     */
    @RequestMapping("/get/mempo/by/login/acct/remote")
    ResultEntity<MemberPO> getMemPOByLoginAcctRemote(@RequestParam("loginacct")String loginacct){

        try {
            //调用本地Service
            MemberPO memberPO = memberService.getMemberPOByLoginAcct(loginacct);
            return ResultEntity.successWithData(memberPO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());
        }
    }
}
